C data types

In the C programming language, data types refers to an extensive system for declaring variables of different types. The language itself provides basic arithmetic types and syntax to build array and compound types. Several headers in the standard library contain definitions of support types, that have additional properties, such as exact size, guaranteed.[1][2]

Contents

Basic types

The C language provides a lot of basic types. Most of them are formed from one of the four basic arithmetic type identifiers in C (char, int, float and double), and optional specifiers (signed, unsigned, short, long). All available basic arithmetic types are listed below:

Type Explanation Type Explanation
char smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned depending on implementation signed char same as char, but guaranteed to be signed.
unsigned char same as char, but guaranteed to be unsigned.
short
short int
signed short
signed short int
short signed integer type. At least 16 bits in size. unsigned short
unsigned short int
same as short, but unsigned.
int
signed int
basic signed integer type. At least 16 bits in size. unsigned
unsigned int
same as int, but unsigned.
long
long int
signed long
signed long int
long signed integer type. At least 32 bits in size. unsigned long
unsigned long int
same as long, but unsigned.
long long
long long int
signed long long
signed long long int
long long signed integer type. At least 64 bits in size. Specified only in C99 version of the standard. unsigned long long
unsigned long long int
same as long long, but unsigned. Specified only in C99 version of the standard.
float single precision floating-point type. Actual properties unspecified, however on most systems this is IEEE 754 single precision floating point format.
double double precision floating-point type. Actual properties unspecified, however on most systems this is IEEE 754 double precision floating point format.
long double extended precision floating-point type. Actual properties unspecified. On most systems this is equivalent either to double, 80-bit floating point format, or IEEE 754 quadruple precision floating-point format.

The actual size of integer types varies by implementation. The only guarantee is that the long long is not smaller than long, which is not smaller than int, which is not smaller than short. Also, int should be the integer type that the target processor is most efficient working with. This allows great flexibility: for example, all types can be 64-bit. However, only several different integer width schemes (data models) are popular and since data model defines how different programs communicate, a uniform data model is used within a given operating system application interface.[3]

The actual size of floating point types also varies by implementation. The only guarantee is that the long double is not smaller than double, which is not smaller than float. Usually, 32-bit and 64-bit IEEE 754 floating point formats are used.

Boolean type

The C language did not have a boolean type until the C99 version of the standard. In C99 the boolean variable has been added as _Bool. Additionally, a new header stdbool.h has been added for compatibility reasons. This header allows programmers to use boolean types in the same way, as in C++ language. The missing identifiers are defined as macros - bool is defined as _Bool, true as 1, false as 0. Additionally, __bool_true_false_are_defined is defined as 1.

Size and pointer difference types

The C language provides the separate types size_t and ptrdiff_t to represent memory related quantities. Existing types were deemed insufficient, because their size is defined according to the target processor arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in stddef.h header (cstddef header in C++).

size_t is used to represent the maximum size of any object (including arrays) in the particular implementation. It is used as the return type of sizeof operator. The maximum size of size_t is provided via SIZE_MAX macro constant which is defined in stdint.h header (cstdint header in C++). It is guaranteed to be at least 65335.

ptrdiff_t is used to represent difference between pointers.

Interface to the properties of the basic types

Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: limits.h header (climits header in C++) defines macros for integer types and float.h header (cfloat header in C++) defines macros for floating-point types. The actual values depend on the implementation.

Properties of integer types
Properties of floating-point types

Fixed width integer types

The C99 standard includes definitions of several new integer types to enhance the portability of programs[2]. The already available basic integer types were deemed insufficient, because their actual sizes are implementation defined and may be vary across different systems. The new types are especially useful in embedded environments where hardware supports usually only several types and that support varies from system to system. All new types are defined in inttypes.h header (cinttypes header in C++) and also are available at stdint.h header (cstdint header in C++). The types can be grouped into the following categories:

The following table summarizes the types and the interface to acquire the implementation details (N refers to the number of bits):

Type category Signed types Unsigned types
Type Minimum value Maximum value Type Minimum value Maximum value
Exact width intN_t INTN_MIN INTN_MAX uintN_t 0 UINTN_MAX
Least width int_leastN_t INT_LEASTN_MIN INT_LEASTN_MAX uint_leastN_t 0 UINT_LEASTN_MAX
Fastest int_fastN_t INT_FASTN_MIN INT_FASTN_MAX uint_fastN_t 0 UINT_FASTN_MAX
Pointer intptr_t INTPTR_MIN INTPTR_MAX uintptr_t 0 UINTPTR_MAX
Maximum width intmax_t INTMAX_MIN INTMAX_MAX uintmax_t 0 UINTMAX_MAX

Printf and scanf format specifiers

The inttypes.h header (cinttypes header in C++) provides features that enhances the functionality of the types defined in stdint.h header. The included things are macros that define printf format string and scanf format string specifiers corresponding to the stdint.h types and several functions for working with intmax_t and uintmax_t types. This header is available only in C99 version of the standard.

Printf format string

All defined macros are in the following format: PRI{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of N, FASTN, LEASTN, PTR, MAX, where N corresponds to the number of bits in the argument.

Scanf format string

All defined macros are in the following format: SCN{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of N, FASTN, LEASTN, PTR, MAX, where N corresponds to the number of bits in the argument.

Functions

Structures

Structures are a way of storing multiple pieces of data in one variable. For example, say we wanted to store the name and birthday of a person in strings, in one variable. We could use a structure to house that data:

struct birthday {
        char name[20];
        int day;
        int month;
        int year;
};

Structures may contain pointers to structs of its own type, which is common in linked datastructures.

A C implementation has freedom to design the memory layout of the struct, with few restrictions; one being that the memory address of the first member will be the same as the address of struct itself. Structs may be initialized or assigned to using compound literals.

Arrays

An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N is indexed by integers from 0 up to and including N-1.

For example:

int cat[10];

Arrays can be initialized with a compound initializer, but not assigned. Arrays are passed to functions by passing a pointer to the first element.

Pointer types

Variables can be declared as being pointers to values of various types, by means of the * type declarator. To declare a variable as a pointer, immediately precede its name with an asterisk.

char *square;
long *circle;

Function pointers

Function pointers allow referencing functions with a particular signature. For example, to store the address of the standard function abs in our variable my_int_f:

int (*my_int_f)(int) = abs;

Function pointers are invoked by name just like normal function calls. Function pointers are separate from pointers and the void * pointer type.

See also

References